package edu.ccut.saturn.studio.deploy;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnProjectUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnDialogUtil;

public class DeployAction implements IObjectActionDelegate {

	private ISelection selection;

	private Shell shell;

	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		shell = targetPart.getSite().getShell();
	}

	public DeployAction() {
	}

	public void run(IAction action) {
		if (selection instanceof IStructuredSelection) {
			IProject project = SaturnProjectUtil
					.getProject((IStructuredSelection) selection);
			String projectName = project.getName();
			if (projectName != null && !Messages.getString("DeployAction.0").equals(projectName)) {  //$NON-NLS-1$
				new DeployDialog(shell, projectName).open();
			} else {
				SaturnDialogUtil.openInformation(shell, Messages.getString("DeployAction.SelectDeployProject"));  //$NON-NLS-1$
			}
		}
	}

	public void selectionChanged(IAction action, ISelection selection) {
		this.selection = selection;
		action.setEnabled(!selection.isEmpty());
	}
}
